<template>
  <div class="pay-callback-container">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付结果</XtxBreadItem>
      </XtxBread>

      <!-- 支付页面 -->
      <div class="pay-result">
        <span v-if="payResult" class="iconfont icon-queren2 green"></span>
        <span v-else class="iconfont icon-shanchu red"></span>

        <p class="tit">订单支付{{ payResult ? '成功' : '失败' }}</p>
        <p class="tip">我们将尽快为您发货, 收货期间请保持手机畅通</p>
        <p>支付方式: <span>支付宝支付</span></p>
        <p>支付金额: <span :class="{price: payResult}">¥{{ order?.payMoney }}</span></p>

        <div class="btn">
          <!-- /member/order 是订单管理页面 /member/order/:id 是订单详情页面 -->
          <XtxButton style="margin-right: 20px"
                     type="primary"
                     @click="$router.push('/member/order')">查看订单
          </XtxButton>
          <XtxButton type="gray" @click="$router.push('/')">进入首页</XtxButton>
        </div>

        <p class="alert">
          <span class="iconfont icon-tip"></span>
          温馨提示: 小兔鲜儿不会以订单异常、系统升级为由要求您点击任何网址链接进行退款操作, 保持资产、谨慎操作
        </p>
      </div>
    </div>
  </div>
</template>

<script>

import API from '@/api/order'
import { useRoute } from 'vue-router'
import { computed, inject, ref } from 'vue'
import emitter from '@/utils/mitt'
import { useStore } from 'vuex'

export default {
  name: 'pay-callback',
  setup() {
    const route = useRoute()
    const store = useStore()
    const order = ref(null)

    // 思考: 是否要定义为计算属性, 这个组件作为一个单独的二级路由组件
    //      访问这个页面是都会触发一次声明周期方法, 我觉得应该不需要
    const payResult = computed(() => {
      return route.query.payResult
    })

    const orderObj = inject('orderObj')
    API.getOrderDetail(route.query.orderId)
        .then((res) => {
          debugger
          order.value = res.result
          // orderObj.orderId = route.query.orderId
          // store.commit('updateOrderId')
          orderObj.orderId++
        })

    // 发出一个自定义事件, 让 pay/index.vue 页面的支付提示框关闭, 并进行路由跳转
    // emitter.emit('close-dialog', { orderId: route.query.orderId })

    return {
      order,
      payResult
    }
  }

}
</script>

<style lang="less" scoped>

.pay-result {
  padding: 100px 0;
  text-align: center;
  background-color: #fff;

  > span {
    font-size: 100px;

    &.green {
      color: #1dc779;
    }

    &.red {
      color: @priceColor;
    }
  }

  p {
    line-height: 40px;
    font-size: 16px;

    .price {
      color: @priceColor;
    }

    &.tit {
      font-size: 24px;
    }

    &.tip {
      color: #999;
    }
  }

  .btn {
    text-align: center;
    padding: 50px 0;
  }

  .alert {
    font-size: 12px;
    color: #999;

    span {
      font-size: 14px;
    }
  }
}
</style>
